package com.panda.pandawash.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.panda.pandawash.R;
import com.panda.pandawash.api.ApplicationConstant;
import com.panda.pandawash.view.StatusBarCompat;

public class FreeWashReasonActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView mBack;

    private Button confirmButton;
    private Button cancelButton;
    private EditText editFreeReason;

    private RelativeLayout friendsLayout;
    private RelativeLayout fixLayout;
    private RelativeLayout othersLayout;
    private RelativeLayout editLayout;

    private ImageView friendsImageView;
    private ImageView fixImageView;
    private ImageView othersImageView;

    private String freeReason = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_free_wash_reason);
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }

        initRes();

    }

    private void initRes() {
        mBack = (ImageView) this.findViewById(R.id.btn_about_return);
        mBack.setOnClickListener(this);

        confirmButton = (Button) findViewById(R.id.btn_free_wash_confirm);
        confirmButton.setOnClickListener(this);
        confirmButton.setBackgroundResource(R.drawable.ic_btn_disable);;
        confirmButton.setClickable(false);
        confirmButton.setTextColor(0x88ffffff);

        cancelButton = (Button) findViewById(R.id.btn_free_wash_cancel);
        cancelButton.setOnClickListener(this);

        friendsLayout = (RelativeLayout) findViewById(R.id.layout_friends);
        friendsLayout.setOnClickListener(this);
        fixLayout = (RelativeLayout) findViewById(R.id.layout_fix);
        fixLayout.setOnClickListener(this);
        othersLayout = (RelativeLayout) findViewById(R.id.layout_others);
        othersLayout.setOnClickListener(this);
        editLayout = (RelativeLayout) findViewById(R.id.layout_edit_reason);

        friendsImageView = (ImageView) findViewById(R.id.friends_select_img);
        fixImageView = (ImageView) findViewById(R.id.fix_select_img);
        othersImageView = (ImageView) findViewById(R.id.others_select_img);

        editFreeReason = (EditText) findViewById(R.id.edit_free_wash_reason);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_about_return:
                finish();
                break;
            case R.id.btn_free_wash_confirm:
                Intent resultIntent = getIntent();
                resultIntent.putExtra("free_reason", freeReason);
                this.setResult(ApplicationConstant.FREE_WASH_REASON_SETTINGS, resultIntent);
                finish();
                break;
            case R.id.btn_free_wash_cancel:
                finish();
                break;

            case R.id.layout_friends:
                selectFreeReason(true, false, false);
                ableConfirmBtn();
                freeReason = getResources().getString(R.string.friends);
                break;
            case R.id.layout_fix:
                selectFreeReason(false, true, false);
                ableConfirmBtn();
                freeReason = editFreeReason.getText().toString();
                break;
            case R.id.layout_others:
                selectFreeReason(false, false, true);
                disableConfirmBtn();
                editFreeReason.addTextChangedListener(watcher);
                break;
            default:
                break;

        }
    }

    private void selectFreeReason(boolean friends, boolean fix, boolean others) {
        if (friends == true && fix == false && others == false) {
            friendsImageView.setVisibility(View.VISIBLE);
            fixImageView.setVisibility(View.INVISIBLE);
            othersImageView.setVisibility(View.INVISIBLE);
            editLayout.setVisibility(View.INVISIBLE);
        } else if (friends == false && fix == true && others == false) {
            friendsImageView.setVisibility(View.INVISIBLE);
            fixImageView.setVisibility(View.VISIBLE);
            othersImageView.setVisibility(View.INVISIBLE);
            editLayout.setVisibility(View.INVISIBLE);
        } else if (friends == false && fix == false && others == true) {
            friendsImageView.setVisibility(View.INVISIBLE);
            fixImageView.setVisibility(View.INVISIBLE);
            othersImageView.setVisibility(View.VISIBLE);
            editLayout.setVisibility(View.VISIBLE);
        }
    }


    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub


        }

        @Override
        public void afterTextChanged(Editable s) {

            if (s.toString().length() > 0) {
                ableConfirmBtn();
                freeReason = editFreeReason.getText().toString();
            } else {
                disableConfirmBtn();
            }

        }
    };

    private void ableConfirmBtn() {
        confirmButton.setBackgroundResource(R.drawable.ic_register_btn);
        confirmButton.setClickable(true);
        confirmButton.setTextColor(0xffffffff);
    }
    private void disableConfirmBtn() {
        confirmButton.setBackgroundResource(R.drawable.ic_btn_disable);
        confirmButton.setClickable(false);
        confirmButton.setTextColor(0x88ffffff);
    }
}
